package com.xuecheng.checkcode.service.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        // 创建新的 RedisTemplate 实例
        RedisTemplate<String, String> template = new RedisTemplate<>();

        // 设置连接工厂
        template.setConnectionFactory(factory);

        // 设置键的序列化器（解决乱码问题的关键）
        template.setKeySerializer(new StringRedisSerializer());

        // 设置值的序列化器
        template.setValueSerializer(new StringRedisSerializer());

        // 初始化
        template.afterPropertiesSet();

        return template;
    }
}